package net.goutalk.glcs.common.ureport;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.bstek.ureport.console.UReportServlet;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.exception.MyException;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Base64;

/***
 * 重写UReport Servlet 管道
 * 新增http-basic鉴权
 * 备注: 无法使用Satoken 鉴权  出此下策。
 * @author tanyujie
 */
@Slf4j
public class DocumentUreportServlet extends UReportServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) {
        String authorization = "Authorization";
        String authenticate = "WWW-Authenticate";
        String basicRealm = "basic realm='no auth'";
        String attrKey = "isLogin";

        //        根据parameter 判断是否有权限
        String parameter = request.getParameter(StpUtil.getTokenName());

//        if(!request.getRequestURI().equals("/ureport/designer")){
//            try {
//                super.service(request, response);
//            } catch (Exception e) {
//                throw new MyException(e.getMessage());
//            }
//        }
        HttpSession session = request.getSession();
        if (session.getAttribute(attrKey) != null) {
            try {
                super.service(request, response);
            } catch (Exception e) {
                throw new MyException(e.getMessage());
            }
        }
        if (StrUtil.isBlank(parameter)) {
            String base6AuthStr = request.getHeader(authorization);
            if (StrUtil.isBlank(base6AuthStr)) {
                response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
                response.addHeader(authenticate, basicRealm);
            } else {
                String authStr = new String(Base64.getDecoder().decode(base6AuthStr.substring(6).getBytes()));

                String[] arr = authStr.split(StringPool.COLON);
                if (arr.length > 0) {
                    if (arr[0].equalsIgnoreCase(GlobalConstant.UREPORT_ACCOUNT) && arr[1].equalsIgnoreCase(GlobalConstant.UREPORT_PASSWORD)) {
                        log.info("ureport2 http-basic 登录成功！");
                    } else {
                        response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
                        response.addHeader(authenticate, basicRealm);
                    }
                } else {
                    response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
                    response.addHeader(authenticate, basicRealm);
                }
            }
        } else {
            Object loginIdByToken = StpUtil.getLoginIdByToken(parameter);


            session.setAttribute(attrKey, true);
            if (ObjectUtil.isNull(loginIdByToken)) {
                throw new MyException("token 错误");
            }
        }


        try {
            super.service(request, response);
        } catch (Exception e) {
            throw new MyException(e.getMessage());
        }
    }
}